#pragma once 

#include<iostream>
#include<memory>
#include"Protocol.hpp"

namespace CalculateNs
{

    enum
    {
        Success,
        DivZeroErro,
        ModZeroError,
        UnKnowError
    };

    class Calculate
    {
    public:
        Calculate()
        {
        }
        std::shared_ptr<Proctocol::Response> Cal(std::shared_ptr<Proctocol::Request> req)
        {
            std::shared_ptr<Proctocol::Response> resp = factory.BuildResponse();
            resp->Setcode(Success);
            switch (req->Getoper())
            {
            case '+':
                resp->Setresult(req->GetX()+req->GetY());
                break;
            case '-':
                resp->Setresult(req->GetX()-req->GetY());
                break;
            case '*':
                resp->Setresult(req->GetX()*req->GetY());
                break;
            case '/':
            {
                if(req->GetY()==0)resp->Setcode(DivZeroErro);
                else
                resp->Setresult(req->GetX()+req->GetY());
                break;
            }
            case '%':
                if(req->GetY()==0)resp->Setcode(ModZeroError);
                else
                resp->Setresult(req->GetX()%req->GetY());
                break;
            default:
                resp->Setcode(UnKnowError);
                break;
            }
            return resp;
        }
        ~Calculate()
        {
        }

    private:
        Proctocol::Factory factory;
    };
}
